<?php
/**
 * @author Garry Willits
 * HtmlDisplay classes define blocks of html for display in web pages
 *
 */
class HtmlDisplay extends Display {
	//TODO - Insert your code here
	protected $dat;
	protected static  $img; 
	protected static  $div;
	protected static  $span;
	protected static  $a;
	
	protected $children =  array();
	public function __construct( ) {
		self::$img = new AggregatedSimpleXmlElement("<img/>");
		self::$div = new AggregatedSimpleXmlElement("<div/>");
		self::$span = new AggregatedSimpleXmlElement("<span/>");
		self::$a = new AggregatedSimpleXmlElement("<a/>");
	}
	
	public function Attribute( $name,$val){
		if( is_a($dat,'SimpleXMLElement')){
			if(!array_key_exists($dat->attributes, $name)){
				$dat->addAttribute($name,$val);
			}
		}
		else{
			$dat[$name]=$val;
		}
	}

	
	/* (non-PHPdoc)
	 * @see Display::finalize()
	 * adds view children into html block
	 */
	public function finalize(){
		foreach( $this->output as $key => $prop ){
			if( is_string($prop) && strpos($prop, '<')>-1){
				$this->replaceXMLWithString( $prop );
			}
			else if(is_array($prop)){
				for( $i = 0; $i<count($prop);$i++){
					if( is_string($prop) && strpos($prop, '<')>-1){
					$this->replaceXMLWithString( $prop );
					}
					
				}
			}
		}
	}
	
	protected function HtmlElement($name,$content=null,$attributes=null){
		$raw = self::$$name->asXML();
		$copy = new AggregatedSimpleXmlElement($raw);
		if(is_string( $content ) && !empty($content)){
			$copy = $copy->addText( $content );
		}
		if(is_array($attributes)){
			foreach($attributes as $key => $val){
				$copy->addAttribute($key, $val);
			}
		}
		return $copy;
	}
	/**
	 * 
	 * Actual implementation used by finalize to parse XMl content
	 * @param unknown_type $key
	 * @param unknown_type $val
	 */
	protected function replaceXMLWithString( $val){
		$this->dat = $this->dat->addChild(new AggregatedSimpleXmlElement($val));
	}
}


?>